home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / et / et3_0-a1.lha / et3 / src / Point.C < prev    next >
C/C++ Source or Header  |  1992-04-27  |  3KB  |  145 lines

  1. #ifdef __GNUG__
  2. #pragma implementation
  3. #endif
  4.  
  5. #define FakeExternalDecl
  6. #include "Point.h"
  7.  
  8. #include "Class.h"
  9. #include "Math.h"
  10. #include "String.h"
  11.  
  12. /* In order to become independent from the order of global constructors
  13.    we fake C++ by using plain old C (which gets initialized by the loader)
  14. */
  15.  
  16. struct C_Point {
  17.     int x, y;
  18. };
  19.  
  20. C_Point gPoint_1 = { -1, -1 },
  21.     gPoint0,
  22.     gPoint1 = { 1, 1 },
  23.     gPoint2 = { 2, 2 },
  24.     gPoint3 = { 3, 3 },
  25.     gPoint4 = { 4, 4 },
  26.     gPoint5 = { 5, 5 },
  27.     gPoint6 = { 6, 6 },
  28.     gPoint7 = { 7, 7 },
  29.     gPoint8 = { 8, 8 },
  30.     gPoint10 = { 10, 10 },
  31.     gPoint16 = { 16, 16 },
  32.     gPoint32 = { 32, 32 },
  33.     gPoint64 = { 64, 64 };
  34.  
  35. SimpleMetaImpl(Point)
  36. {
  37.     len= len;
  38.     if (! isptr)
  39.     sprintf(buf, "%s", ((Point*) addr)->AsString());
  40. }
  41.  
  42. Point Min(Point p1, Point p2)
  43. {
  44.     return Point(Math::Min(p1.x, p2.x), Math::Min(p1.y, p2.y));
  45. }
  46.  
  47. Point Max(Point p1, Point p2)
  48.     return Point(Math::Max(p1.x, p2.x), Math::Max(p1.y, p2.y));
  49. }
  50.  
  51. Point Range (Point lb, Point ub, Point x)
  52. {
  53.     return Point(Math::Range(lb.x, ub.x, x.x), Math::Range(lb.y, ub.y, x.y));
  54. }
  55.  
  56. Point Point::Half() const
  57. {
  58.     return Point(x >> 1, y >> 1);
  59. }
  60.  
  61. Point Half (Point p)
  62. {
  63.     return Point(p.x/2, p.y/2);
  64. }
  65.  
  66. Point Abs(Point p)
  67. {
  68.     return Point(Math::Abs(p.x), Math::Abs(p.y));
  69. }
  70.  
  71. Point Sign(Point p)
  72. {
  73.     return Point(Math::Sign(p.x), Math::Sign(p.y));
  74. }
  75.  
  76. Point Scale(Point p, Point num, Point denom)
  77. {
  78.     Point r;
  79.     
  80.     if (denom.x)
  81.     r.x= (int)((float)(p.x*num.x) / (float)denom.x + 0.5);
  82.     if (denom.y)
  83.     r.y= (int)((float)(p.y*num.y) / (float)denom.y + 0.5);
  84.     
  85.     return r;
  86. }
  87.  
  88. Point Scale(Point p, float sx, float sy)
  89. {
  90.     return Point((float)((p.x*sx)+0.5), (float)((p.y*sy)+0.5));
  91. }
  92.  
  93. float Phi(Point p)
  94. {
  95.     return Math::Atan2(p.y, p.x);
  96. }
  97.  
  98. float Length(Point p)
  99. {
  100.     return (float) Math::Hypot(p.x, p.y);
  101. }
  102.  
  103. Point PolarToPoint(double ang, double fx, double fy)
  104. {
  105.     double si, co;
  106.     Math::Sincos(ang, &si, &co);
  107.     return Point((int)(fx*co+0.5), (int)(fy*si+0.5));
  108. }
  109.  
  110. Point polartopoint(double ang, double fx, double fy)
  111. {
  112.     double si, co;
  113.     Math::Sincos(ang, &si, &co);
  114.     return Point((int)(fx*co+0.5), (int)(fy*si+0.5));
  115. }
  116.  
  117. void Swap(Point &p1, Point &p2)
  118. {
  119.     Point tmp= p1;
  120.     p1= p2;
  121.     p2= tmp;
  122.   
  123. Point Flip(Point &p)
  124. {
  125.     return Point(p.y, p.x);
  126.   
  127. OStream& operator<< (OStream &s, Point p)
  128. {
  129.     return s << p.x SP << p.y SP;
  130. }
  131.  
  132. IStream& operator>> (IStream& s, Point& p)
  133. {
  134.     return s >> p.x >> p.y;
  135. }
  136.  
  137. char *Point::AsString() const
  138. {
  139.     return form("x: %d y: %d", x, y);
  140. }
  141.  
  142.